package com.qq.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qq.bean.Users;
import com.qq.dao.BaseDao;
import com.qq.dao.UserDao;

public class UserDaoImpl implements UserDao {

	@Override
	public boolean addUsers(Users user) {
		Connection con = null;
        PreparedStatement ps = null;
        String sql = "INSERT INTO users (account, password, email, phone, address, role) VALUES (?, ?, ?, ?, ?, 'user')";
        try {
        	con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
            ps.setString(1, user.getAccount());
            ps.setString(2, user.getPassword());  // 密码应加密存储，但这里暂时简化处理
            ps.setString(3, user.getEmail());
            ps.setString(4, user.getPhone());
            ps.setString(5, user.getAddress());
//            ps.setString(6, user.getRole());
            return ps.executeUpdate() > 0;  // 如果插入成功，返回true
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(con, ps, null);
        }
        return false;
	}

	@Override
	public Users getUsersById(int userId) {
		Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
		String sql = "SELECT * FROM users WHERE userid = ?";
        try {
        	con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
            ps.setInt(1, userId);
            rs = ps.executeQuery();
            if (rs.next()) {
                Users user = new Users();
                user.setUserId(rs.getInt("userid"));
                user.setAccount(rs.getString("account"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setRole(rs.getString("role"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(con, ps, rs);
        }
        return null; 
	}

	@Override
	public Users getUsersByUsersname(String username) {
		Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
		String sql = "SELECT * FROM users WHERE account = ?";
        try {
        	con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
            ps.setString(1, username);
            rs = ps.executeQuery();
            if (rs.next()) {
                Users user = new Users();
                user.setUserId(rs.getInt("userid"));
                user.setAccount(rs.getString("account"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setRole(rs.getString("role"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(con, ps, rs);
        }
        return null;
	}

	@Override
	public boolean updateUsers(Users user) {
		Connection con = null;
        PreparedStatement ps = null;
		String sql = "UPDATE users SET account = ?, password = ?, email = ?, phone = ?, address = ?, role = ? WHERE userid = ?";
        try {
        	con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
            ps.setString(1, user.getAccount());
            ps.setString(2, user.getPassword());  // 密码更新同样需要加密
            ps.setString(3, user.getEmail());
            ps.setString(4, user.getPhone());
            ps.setString(5, user.getAddress());
            ps.setString(6, user.getRole());
            ps.setInt(7, user.getUserId());
            return ps.executeUpdate() > 0;  // 如果更新成功，返回true
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(con, ps, null);
        }
        return false;
	}

	@Override
	public boolean deleteUsers(int userId) {
		Connection con = null;
        PreparedStatement ps = null;
		String sql = "DELETE FROM users WHERE userid = ?";
        try {
        	con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
            ps.setInt(1, userId);
            return ps.executeUpdate() > 0;  // 如果删除成功，返回true
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(con, ps, null);
        }
        return false;
	}

	@Override
	public boolean validateUsers(String username, String password) {
		Connection con = null;
        PreparedStatement ps = null;
		String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
        try {
        	con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);  // 密码应加密存储，这里简化处理
            ResultSet rs = ps.executeQuery();
            return rs.next();  // 如果查询结果有记录，表示用户名和密码匹配
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(con, ps, null);
        }
        return false;
	}

	@Override
	public List<Users> getAllUsers() {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Users> users = new ArrayList<>();
        String sql = "SELECT * FROM users";
        try {
        	con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
            while (rs.next()) {
                Users user = new Users();
                user.setUserId(rs.getInt("userid"));
                user.setAccount(rs.getString("account"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setRole(rs.getString("role"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(con, ps, rs);
        }
        return users;
	}

}
